/*
 * Activiti Modeler component part of the Activiti project
 * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

/*
 * Assignment(分配)
 *
 */

var KisBpmAssignmentCtrl = [ '$scope', '$modal', function($scope, $modal) {

	// Config for the modal window
	var opts = {
		template:  'editor-app/configuration/properties/assignment-popup.html?version=' + Date.now(),
		scope: $scope
	};

	// Open the dialog(打开对话框)
	$modal(opts);
}];

var KisBpmAssignmentPopupCtrl = [ '$scope', function($scope) {
	//父页面（绘制表单页面属性代入弹出属性选择页面的数据）
	console.log($scope.property,'属性值');
	console.log($scope.property.value);
	//禁用按钮；为true的时候开始禁用
	$scope.isDisabled=false;
	$scope.data = {
		userId: '',
		Principalinfo_id:'',
		CandidateuserIndex: '',
		CandidateGroupsIndex: ''

	};

	//负责人信息
	$scope.principal_info=[
		{Principalinfo_id:"提交部门负责人",Principalinfo_name:"提交部门负责人"},
		{Principalinfo_id:"提交人部门分管领导",Principalinfo_name:"提交人部门分管领导"},
		{Principalinfo_id:"中心主任",Principalinfo_name:"中心主任"},
	];
	//默认绑定的数据
	$scope.com={
		Principalinfo_id:2
	};
	// $scope.institution.display="true";
	console.log($scope.assignment);
	// Put json representing assignment on scope(如果分配用户的值全都有值)
	if ($scope.property.value !== undefined && $scope.property.value !== null
		&& $scope.property.value.assignment !== undefined
		&& $scope.property.value.assignment !== null)
	{
		//赋值
		$scope.assignment = $scope.property.value.assignment;
	} else {
		$scope.assignment = {};
	}
	//候选人为空
	if ($scope.assignment.candidateUsers == undefined || $scope.assignment.candidateUsers.length == 0)
	{
		//定义为数组，并追加空value
		$scope.assignment.candidateUsers = [{value: ''}];
	}

	// Click handler for + button after enum value
	var userValueIndex = 1;
	$scope.addCandidateUserValue = function(index) {
		//添加候选人填写框，并赋初初始值
		$scope.assignment.candidateUsers.splice(index + 1, 0, {value: 'value ' + userValueIndex++});
	};

	// Click handler for - button after enum value
	$scope.removeCandidateUserValue = function(index) {
		$scope.assignment.candidateUsers.splice(index, 1);
	};

	if ($scope.assignment.candidateGroups == undefined || $scope.assignment.candidateGroups.length == 0)
	{
		$scope.assignment.candidateGroups = [{value: ''}];
	}

	var groupValueIndex = 1;
	$scope.addCandidateGroupValue = function(index) {
		//添加候选组填写框，并赋初初始值
		$scope.assignment.candidateGroups.splice(index + 1, 0, {value: 'value ' + groupValueIndex++});
		//$scope.date = index+1;
	};

	// Click handler for - button after enum value
	$scope.removeCandidateGroupValue = function(index) {
		$scope.assignment.candidateGroups.splice(index, 1);
	};
	//弹出(选择)代理人
	$scope.onChangeUser = function () {

		document.getElementById('changeMenTree').style.display = "block";
	};
	//弹出(选择)候选人
	$scope.onCandidateuser = function (index){
		CandidateuserIndex = index;
		document.getElementById('changeCandidate').style.display = "block";
	}
	//弹出(选择)候选组
	$scope.onCandidateGroups = function (index){
		CandidateGroupsIndex = index;
		console.log(CandidateGroupsIndex);
		document.getElementById('changeCandidateGroup').style.display = "block";
	}
	//保存
	$scope.save = function() {

		$scope.property.value = {};
		handleAssignmentInput($scope);
		//申请负责人赋值

		// if(!$scope.assignment.Principalinfo_id){
		// 	$scope.assignment.assignee=$scope.data.Principalinfo_id;
		// }

		//代理人赋值++

		if(!$scope.assignment.assignee){

			$scope.assignment.assignee = $scope.data.PersonnelFiles_id;
		}


		//候选人赋值
		// if( $scope.assignment.candidateUsers){
		// 		// $scope.assignment.candidateUsers.forEach((item,index)=>{
		// 		// 	item.value = item.showInfo;
		// 	$scope.assignment.candidateUsers=$$scope.data.CandidateGroupsIndex[0];
		// 	}
		// }
		// else{
		// 	$scope.assignment.candidateUsers = [{value:''}]
		// };
		// //候选组赋值
		// if($scope.assignment.candidateGroups){
		// 		$scope.assignment.candidateGroups.forEach((item,index)=>{
		// 			item.value = item.showInfo;
		// 	})
		// }else{
		// 	$scope.assignment.candidateGroups = [{value:''}]
		// }
		console.log($scope.assignment,'保存+++++++++++++++++');
		$scope.property.value.assignment = $scope.assignment;

		$scope.updatePropertyInModel($scope.property);
		$scope.close();
		console.log($scope.property.value );
	};

	// Close button handler
	// 关闭
	$scope.close = function() {
		handleAssignmentInput($scope);
		$scope.property.mode = 'read';
		$scope.$hide();
	};
	//确定
	$scope.onSaveChangeUser = function (action){
		console.log(action);
	}
	//关闭选择代理人的页面
	$scope.closeChangeUser = function(action) {
		if(action == 'cancel' || action == 'close'){
			//关闭弹出框（隐藏）
			document.getElementById('changeMenTree').style.display = "none";
		}else{
			//赋值（iframe父页面调用子页面的方法）
			var iframe = window.frames["mini-iframe-6"].onOk();
			console.log(iframe);
			if(iframe){
				//人员ID(保存时保存的值)
				$scope.assignment.assignee = iframe.PersonnelFiles_id;
				//传值
				// console.log(iframe.PersonnelFiles_id);
				// var Pid = iframe.PersonnelFiles_id;
				// localStorage.setItem("Pid",Pid);
				//弹出框隐藏
				document.getElementById('changeMenTree').style.display = "none";
			}else{
				//调用弹出框
				window.frames["mini-iframe-6"].alertBox();
			}
		}
	};
	//关闭选择候选人的页面
	$scope.closeChangeCandidate = function(action) {
		if(action == 'cancel' || action == 'close'){
			//弹出框隐藏
			document.getElementById('changeCandidate').style.display = "none";
		}else{
			//赋值（iframe父页面调用子页面的方法）
			var iframe = window.frames["mini-iframe-7"].onOk();
			if(iframe){
				var candidateUsersArr = $scope.assignment.candidateUsers;
				candidateUsersArr[CandidateuserIndex].value = iframe.perName;
				candidateUsersArr[CandidateuserIndex].showInfo = iframe.perId;
				//弹出框隐藏
				document.getElementById('changeCandidate').style.display = "none";
			}else{
				//调用弹出框
				window.frames["mini-iframe-7"].alertBox();
			}
		}
	};
	//关闭选择候选组的页面
	$scope.closeCandidateGroup = function(action) {
		console.log(action);
		if(action == 'cancel' || action == 'close'){
			//弹出框隐藏
			document.getElementById('changeCandidateGroup').style.display = "none";
		}else{
			//赋值（iframe父页面调用子页面的方法）
			var iframe = window.frames["mini-iframe-8"].onOk();
			console.log(window.frames["mini-iframe-8"]);
			if(iframe){
				console.log(iframe,'候选组');
				$scope.assignment.candidateGroups[CandidateGroupsIndex].value = iframe.roleName;
				$scope.assignment.candidateGroups[CandidateGroupsIndex].showInfo = iframe.roleId;
				//弹出框隐藏
				document.getElementById('changeCandidateGroup').style.display = "none";
			}else{
				window.frames["mini-iframe-8"].alertBox();
			}
		}
	};
	//手动分配输入
	var handleAssignmentInput = function($scope) {
		if ($scope.assignment.candidateUsers)
		{
			var emptyUsers = true;
			var toRemoveIndexes = [];
			//候选人
			for (var i = 0; i < $scope.assignment.candidateUsers.length; i++)
			{
				if ($scope.assignment.candidateUsers[i].value != '')
				{
					emptyUsers = false;
				}
				else
				{
					toRemoveIndexes[toRemoveIndexes.length] = i;
				}
			}

			for (var i = 0; i < toRemoveIndexes.length; i++)
			{
				$scope.assignment.candidateUsers.splice(toRemoveIndexes[i], 1);
			}
			//根据是否是空值(为空：true)
			if (emptyUsers)
			{
				$scope.assignment.candidateUsers = undefined;
			}
		}
		//候选组
		if ($scope.assignment.candidateGroups)
		{
			var emptyGroups = true;
			var toRemoveIndexes = [];
			for (var i = 0; i < $scope.assignment.candidateGroups.length; i++)
			{
				if ($scope.assignment.candidateGroups[i].value != '')
				{
					emptyGroups = false;
				}
				else
				{
					toRemoveIndexes[toRemoveIndexes.length] = i;
				}
			}

			for (var i = 0; i < toRemoveIndexes.length; i++)
			{
				$scope.assignment.candidateGroups.splice(toRemoveIndexes[i], 1);
			}

			if (emptyGroups)
			{
				$scope.assignment.candidateGroups = undefined;
			}
		}
	};
}];